package com.mall.common.util;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 提供一些系统中使用到的共用方法
 * 比如获得当前用户信息
 * @author zhenglong
 */
@Component
public class AppContext implements ServletContextAware {

	private static ServletContext servletContext;
	
	public static String getAppRealPath(String path) {
		return servletContext.getRealPath(path);
	}
	
	public static HttpServletRequest getRequest(){
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		if(ra == null){
			return null;
		}
		HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();
		return request;
	}
	
	/**
	 * 获取上下文路径
	 * @return
	 */
	public static String getContextPath(){
		return servletContext.getContextPath();
	}
	
	/**
	 * 获取上下文路径
	 * @return
	 */
	public static ServletContext getContext(){
		return servletContext.getContext("/");//ServletActionContext.getServletContext();
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	public static void resetServletContextAttr(String key, Object value){
		servletContext.setAttribute(key, value);
	}
	
	public static String getRemoteIp() {
		HttpServletRequest request = AppContext.getRequest();
		
		String ip = request.getHeader("X-Real-IP");
		
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("X-Forwarded-For");;
		}
		
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        
        return ip;
	}
	
	public static Object getSessionAttr(String key) {
		HttpSession session = getRequest().getSession(
				false);
		if (session == null) {
			return null;
		} else {
			return session.getAttribute(key);
		}
	}
	
	public static Cookie getCookie(String name) {
		Cookie[] cookies = getRequest().getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				if (c.getName().equals(name)) {
					return c;
				}
			}
		}
		return null;
	}
	
	public static String getSessionId(boolean isCreate) {
		HttpSession session = getRequest().getSession(
				isCreate);
		if (session == null) {
			return null;
		} else {
			return session.getId();
		}
	}

	public static void setSessionAttr(String key, Object value) {
		HttpSession session = getRequest().getSession(false);
		if(session != null){
			session.setAttribute(key, value);
		}
	}
	
	public static void removeAttribute(String key) {
		HttpSession session = getRequest().getSession(false);
		if(session != null){
			session.removeAttribute(key);
		}
	}
	
	/**
	 * 是否ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjax(HttpServletRequest request){
		String xRequestedWith = request.getHeader("X-Requested-With");
		if (xRequestedWith != null && xRequestedWith.equalsIgnoreCase("XMLHttpRequest")) {
			return true;
		}
		return false;
	}
}
